frappe.listview_settings['Tongtool Order'] = {
    onload: function(listview) {
        // 批量刷新订单数据
        listview.page.add_action_item(__('Bulk Refresh Orders'), function() {
            let selected_docs = listview.get_checked_items();
            
            if (selected_docs.length === 0) {
                frappe.msgprint(__('Please select at least one order'));
                return;
            }
            
            if (selected_docs.length > 100) {
                frappe.confirm(
                    __('You have selected {0} orders. This may take some time. Use queue processing?', [selected_docs.length]),
                    function() {
                        bulk_refresh_orders(selected_docs, true);
                    },
                    function() {
                        bulk_refresh_orders(selected_docs, false);
                    }
                );
            } else {
                bulk_refresh_orders(selected_docs, false);
            }
        });
        
        // 批量重新匹配SKU
        listview.page.add_action_item(__('Bulk Rematch SKU'), function() {
            let selected_docs = listview.get_checked_items();
            
            if (selected_docs.length === 0) {
                frappe.msgprint(__('Please select at least one order'));
                return;
            }
            
            frappe.call({
                method: 'tongtool_integration.sync.order_sync.rematch_sku_for_orders',
                args: {
                    order_names: JSON.stringify(selected_docs.map(d => d.name))
                },
                callback: function(r) {
                    if (r.message && r.message.success) {
                        listview.refresh();
                    }
                }
            });
        });
        
        // 刷新未匹配订单
        listview.page.add_action_item(__('Refresh Unmatched Orders'), function() {
            frappe.call({
                method: 'tongtool_integration.sync.order_sync.bulk_refresh_orders',
                args: {
                    filters: JSON.stringify({"match_status": "未匹配"}),
                    use_queue: true
                },
                callback: function(r) {
                    if (r.message && r.message.success) {
                        listview.refresh();
                    }
                }
            });
        });
        
        // 刷新缺失字段订单
        listview.page.add_action_item(__('Refresh Missing Data Orders'), function() {
            frappe.call({
                method: 'tongtool_integration.sync.order_sync.bulk_refresh_orders',
                args: {
                    // 不传filters，使用默认的查询逻辑
                    use_queue: true
                },
                callback: function(r) {
                    if (r.message && r.message.success) {
                        listview.refresh();
                    }
                }
            });
        });
    }
};

function bulk_refresh_orders(selected_docs, use_queue) {
    frappe.call({
        method: 'tongtool_integration.sync.order_sync.bulk_refresh_orders',
        args: {
            order_names: JSON.stringify(selected_docs.map(d => d.name)),
            use_queue: use_queue
        },
        callback: function(r) {
            if (r.message && r.message.success) {
                frappe.listview_settings['Tongtool Order'].refresh();
            }
        }
    });
}
